﻿#include "changepassword.h"
#include "qsqlquery.h"
#include "ui_ChangePassword.h"
#include<QMessageBox>
#include<QRegularExpression>
#include<QRegularExpressionValidator>
ChangePassword::ChangePassword(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChangePassword)
{
    ui->setupUi(this);

    this->move(QPoint(800, 0));
    opacity = new QGraphicsOpacityEffect(this);
    opacity->setOpacity(0);
    //ui->frame->setGraphicsEffect(opacity);
    QString style;
    style = "background-color:white;border-radius:" + QString::asprintf("%d", cornerRadius) + "px";
    bgWidget = new QWidget(this);
    bgWidget->lower();
    bgWidget->resize(this->size());
    bgWidget->setStyleSheet(style);
    bgWidget->show();

    sheildLayer = new SheildLayer(this->parentWidget());
    sheildLayer->resize(this->parentWidget()->size());
    sheildLayer->setGraphicsEffect(opacity);
    sheildLayer->setMouseTracking(true);
    connect(sheildLayer, &SheildLayer::clicked, this, [=](){sildeOut();setFocus();});

//    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
//    shadow->setBlurRadius(30);
//    shadow->setColor(QColor(0, 0, 0));
//    shadow->setOffset(0, 0);
//    ui->frame->setGraphicsEffect(shadow);
   // ui->widget->lower();
    this->setFocusPolicy(Qt::ClickFocus);
    this->setMouseTracking(true);
    bgWidget->setMouseTracking(true);
    sheildLayer->setMouseTracking(true);

    email= nullptr;
    //TODO 默认选中
    ui->confirm->setDefault(1);


    QAction * usericon=new QAction(this);
    usericon->setIcon(QIcon(":/images/user-circle.png"));
    QAction * passwordLock=new QAction(this);
    passwordLock->setIcon(QIcon(":/images/lock-on.png"));

    ui->Account->setPlaceholderText("身份证号：");
    ui->newpwd->setPlaceholderText("新密码：");
    ui->Account->addAction(usericon,QLineEdit::LeadingPosition);
    ui->newpwd->addAction(passwordLock,QLineEdit::LeadingPosition);

    //设置验证码格式
    ui->code->setInputMask("   9       9        9        9");
    connect(ui->Account,&QLineEdit::returnPressed,this,&ChangePassword::processRet);
    connect(ui->newpwd,&QLineEdit::returnPressed,this,&ChangePassword::processRet);
    connect(ui->code,&QLineEdit::returnPressed,this,&ChangePassword::processRet);

}

ChangePassword::~ChangePassword()
{
    delete ui;
}

//TODO ID -> email -> change pwd
void ChangePassword::on_confirm_clicked()
{

    QString code = ui->code->text();
    code.remove(QRegularExpression("\\s"));
    QString account = ui->Account->text();
    if(email!=nullptr){
        if(email->check_email()&&email->code!=nullptr&&code==email->code->GetCode()){
            QString newpws = ui->newpwd->text();
            QSqlQuery query = QSqlQuery(db);
            QString command = "UPDATE Reader SET password = '"+newpws+"'where ID ='"+account+"'";
            query.exec(command);
            QMessageBox::information(this,"提示","修改成功！");
            ui->Account->clear();
            ui->code->clear();
            ui->newpwd->clear();
            this->sildeOut();

        }else if (code==""){
            QMessageBox::information(this,"提示","请输入验证码！");
        }
        else{
            QMessageBox::information(this,"提示","您的验证码错误，请重新输入！");
        }
    }
}


void ChangePassword::on_sendcode_clicked()
{
    QString code = ui->code->text();
    QString account = ui->Account->text();
    QSqlQuery query = QSqlQuery(db);
    QString command = "SELECT * from Reader where ID = '"+account+"'";
    query.exec(command);
    if(!query.next()){
        QMessageBox::information(this,"提示","您输入的账号有误，请重新输入");
    }else{
        QString address =query.value("email").toString();
        email= new Email(address);
        if(email->check_email()){
            email->SendCode();
        }else{
             QMessageBox::information(this,"提示","您的email有误，请重新输入！");
        }
    }
    db.close();
}


void ChangePassword::on_cancel_clicked()
{
    sildeOut();
}
void ChangePassword::processRet(){
    QLineEdit * returnPressedSender=(QLineEdit*)sender();
    if(returnPressedSender==ui->Account)
        ui->newpwd->setFocus();
    else if(returnPressedSender==ui->newpwd){
        ui->sendcode->click();
        ui->code->setFocus();
    }
    else if(returnPressedSender==ui->code){
        ui->confirm->click();}

}
void ChangePassword::sildeIn(){
    if(curAni){
        curAni->stop();
        curAni->deleteLater();
        curAni = nullptr;
    }
    onShown = true;
    sheildLayer->raise();
    sheildLayer->setEnabled(true);
    this->raise();
    sheildLayer->show();
    QParallelAnimationGroup *inGroup = new QParallelAnimationGroup(this);
    QPropertyAnimation *slideInAni = new QPropertyAnimation(this, "pos", this);
    slideInAni->setStartValue(this->pos());
    slideInAni->setEndValue(QPoint(395,0));
    slideInAni->setDuration(1000);
    slideInAni->setEasingCurve(QEasingCurve::InOutExpo);

    QPropertyAnimation *fadeInAni = new QPropertyAnimation(opacity, "opacity", this);
    fadeInAni->setStartValue(opacity->opacity());
    //> note: DO NOT CHANGE 0.99 TO 1!!!!!
    //>       Will cause unexpected position shift (maybe qt's bug)
    fadeInAni->setEndValue(0.99);
    fadeInAni->setDuration(750);

    inGroup->addAnimation(slideInAni);
    inGroup->addAnimation(fadeInAni);
    connect(inGroup, &QParallelAnimationGroup::finished, this, [=](){this->curAni = nullptr;});
    inGroup->start();
    curAni = inGroup;
}

void ChangePassword::sildeOut(){
    if(curAni){
        curAni->stop();
        curAni->deleteLater();
        curAni = nullptr;
    }
    onShown = false;
    sheildLayer->setEnabled(false);
    QParallelAnimationGroup *outGroup = new QParallelAnimationGroup(this);
    QPropertyAnimation *slideOutAni = new QPropertyAnimation(this, "pos", this);
    slideOutAni->setStartValue(this->pos());
    slideOutAni->setEndValue(QPoint(800, 0));
    slideOutAni->setDuration(1000);
    slideOutAni->setEasingCurve(QEasingCurve::InOutExpo);
    QPropertyAnimation *fadeOutAni = new QPropertyAnimation(opacity, "opacity", this);
    fadeOutAni->setStartValue(opacity->opacity());
    fadeOutAni->setEndValue(0);
    fadeOutAni->setDuration(750);

    outGroup->addAnimation(slideOutAni);
    outGroup->addAnimation(fadeOutAni);
    connect(outGroup, &QPropertyAnimation::finished, this, [=](){this->curAni = nullptr;sheildLayer->hide();});

    outGroup->start();
    curAni = outGroup;
}

